﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RussiaLibrary
{
    public class TPolygon : Class_PolygonBase
    {
        public override bool Init(byte[,] Bytes_TetrisByte)//初始化T型
        {
            int_State = Class_Common.GetRandomInt() % 4;
            switch (int_State)//T形方块初始化。
            {
                case 0:// |-
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(0, 4), new KeyValuePair<int, int>(1, 4),
                                                      new KeyValuePair<int, int>(2, 4), new KeyValuePair<int, int>(1, 5));
                case 1:// ↑
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(1, 4), new KeyValuePair<int, int>(1, 5),
                                                      new KeyValuePair<int, int>(1, 6), new KeyValuePair<int, int>(0, 5));
                case 2:// -|
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(2, 5), new KeyValuePair<int, int>(1, 5),
                                                      new KeyValuePair<int, int>(0, 5), new KeyValuePair<int, int>(1, 4));
                case 3:// ↓
                    return InitBase(Bytes_TetrisByte, new KeyValuePair<int, int>(0, 6), new KeyValuePair<int, int>(0, 5),
                                                      new KeyValuePair<int, int>(0, 4), new KeyValuePair<int, int>(1, 5));
                default:
                    break;
            }
            return false;
        }
        public override void Change(byte[,] Bytes_TetrisByte)
        {
            bool canChange = true;
            if (points[1].Key + 2 < Class_Common.Length && points[0].Key - 2 > 0 && points[0].Value + 2 < Class_Common.Width && points[0].Value - 2 > 0)//防止越界
            {
                for (int i = points[1].Key - 1; i < points[1].Key + 2; i++)//4x4的方格作为变形框，该方格里面有任何一处被占用，那么则不能变形
                {
                    for (int j = points[1].Value - 1; j <= points[1].Value + 2; j++)
                    {
                        if (Bytes_TetrisByte[i, j] == 2)
                        {
                            canChange = false;
                        }
                    }
                }
               // canChange = false;
            }
            else
            {
                canChange = false;
            }
            if (true == canChange)
            {
                switch (int_State)
                {
                    case 0:
                        {

                            List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                            for (int i = 0; i < 4; i++)//i表示第几个点的处理
                            {
                                Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                                KeyValuePair<int, int> tempPoint;
                                switch (i)
                                {
                                    case 0:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 1:
                                        Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                        newPoints.Add(points[i]);
                                        break;
                                    case 2:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    case 3:
                                        tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                        newPoints.Add(tempPoint);
                                        Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                        break;
                                    default:
                                        break;
                                }

                        }
                        int_State = 3;
                        points = newPoints;
                    }
                    break;
                case 1:
                    {
                        KeyValuePair<int, int> tempPoint;
                        List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                        for (int i = 0; i < 4; i++)//i表示第几个点的处理
                        {
                            Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                            switch (i)
                            {
                                case 0:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value + 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                case 1:
                                    Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                    newPoints.Add(points[i]);
                                    break;
                                case 2:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                case 3:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                default:
                                    break;
                            }

                        }
                        int_State = 0;
                        points = newPoints;
                    }
                    break;
                case 2:
                    {
                        KeyValuePair<int, int> tempPoint;
                        List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                        for (int i = 0; i < 4; i++)//i表示第几个点的处理
                        {
                            Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                            switch (i)
                            {
                                case 0:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value - 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                case 1:
                                    Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                    newPoints.Add(points[i]);
                                    break;
                                case 2:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value + 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                case 3:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value + 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                default:
                                    break;
                            }

                        }
                        int_State = 1;
                        points = newPoints;
                    }
                    break;
                case 3:
                    {
                        KeyValuePair<int, int> tempPoint;
                        List<KeyValuePair<int, int>> newPoints = new List<KeyValuePair<int, int>>();
                        for (int i = 0; i < 4; i++)//i表示第几个点的处理
                        {
                            Bytes_TetrisByte[points[i].Key, points[i].Value] = 0;
                            switch (i)
                            {
                                case 0:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key + 1, points[i].Value - 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                case 1:
                                    Bytes_TetrisByte[points[i].Key, points[i].Value] = 1;
                                    newPoints.Add(points[i]);
                                    break;
                                case 2:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value + 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                case 3:
                                    tempPoint = new KeyValuePair<int, int>(points[i].Key - 1, points[i].Value - 1);
                                    newPoints.Add(tempPoint);
                                    Bytes_TetrisByte[tempPoint.Key, tempPoint.Value] = 1;
                                    break;
                                default:
                                    break;
                            }

                            }
                            int_State = 2;
                            points = newPoints;
                        }
                        break;
                    default:
                        break;
                }  
            
            }
        }
    }
}
